import os
import os.path
import stat

from paste.script.templates import Template, var
from tempita import paste_script_template_renderer
from paste.deploy.converters import asbool

vars = [
    var('version', 'Version (like 0.1)', '0.0'),
    var('description', 'One-line description of the package', ''),
    var('long_description', 'Multi-line description (in reST)', ''),
    var('keywords', 'Space-separated keywords/tags', ''),
    var('author', 'Author name', ''),
    var('author_email', 'Author email', ''),
    var('url', 'URL of homepage', ''),
    var('license_name', 'License name', ''),
    var('zip_safe', 'y/n: if the package can be distributed as a .zip file',
        default='n'),
    var('configobj', 'y/n: Include ConfigObj ini file reader/writer', default='y'),
    var('euclid', 'y/n: Include euclid 2D/3D maths library', default='y'),
    var('virtualenv_bootstrap', 'y/n: Include a virtualenv bootstrap script', 
        default='y'),
]

class BaseTemplate(Template):
    vars = vars

    template_renderer = staticmethod(paste_script_template_renderer)

    def pre(self, command, output_dir, vars):
        """Called before template is applied."""
        vars['ProjectName'] = "".join(
                [p.capitalize() for p in 
                 vars['project'].replace('-', '_').replace(' ', '').split('_')]
        )

        vars['version'] = vars.get('version', '0.1')

        as_bools = ('zip_safe', 
                    'configobj',
                    'euclid',
                    'virtualenv_bootstrap',
        )
        for key in as_bools:
            vars[key] = asbool(vars.get(key))

        vars['minimal'] = True
        vars['game'] = False
 
    def post(self, command, output_dir, vars):
        run_file = os.path.join(output_dir, 'run.py')
        os.chmod(run_file, stat.S_IEXEC | stat.S_IREAD | stat.S_IWRITE)




